package com.MartynovichIgor.KnowledgeTest.tools.usertools;

import com.MartynovichIgor.KnowledgeTest.tools.admintools.CurrentDate;
import com.MartynovichIgor.KnowledgeTest.tools.filetools.FileSaver;
import com.MartynovichIgor.KnowledgeTest.resources.ProjectPath;
import com.MartynovichIgor.KnowledgeTest.tools.filetools.FileLoader;
import com.MartynovichIgor.KnowledgeTest.tools.testtools.Parser;
import java.io.*;
import java.util.ArrayList;

/**
 * Created by:
 * User: Igor Martynovich and Guzenko Igor
 * Date: 25.10.13
 */
public class UserSaver {
    public static final String USER_INFO_FILE =
            "src\\com\\MartynovichIgor\\KnowledgeTest\\userinfo.txt";
    public static final String USER_STATISTIC_FILE="src\\com\\MartynovichIgor\\KnowledgeTest\\statistics.txt" ;
    private FileSaver infoSaver;
    private Parser parser;
    private FileSaver statisticSaver;
    private String userInfoFile;
    private String userStatisticFile;
    private FileLoader loader;

    public UserSaver(){
        this.userInfoFile =String.format("%s%s",new ProjectPath().getProjectPath(),USER_INFO_FILE);
        this.userStatisticFile=String.format("%s%s",new ProjectPath().getProjectPath(),USER_STATISTIC_FILE);
        this.infoSaver = new FileSaver(userInfoFile);
        this.statisticSaver = new FileSaver(userStatisticFile);
        this.parser = new Parser();
        this.loader = new FileLoader();

    }

    /**
     * Метод сохраняет информацию о пользователе.
     *
     * @param userInfo

     * @throws IOException
     */
    public void saveUserInfo(String userInfo,String login,int scores) throws IOException {
        rewriteUserInfo(userInfo,login, scores);
    }

    /**
     * Метод переписывает историю если такой пользователь уже сохранялся.
     * @param userInfo
     * @throws IOException
     */
    private void rewriteUserInfo(String userInfo,String login,int scores) throws IOException {
        String loadedInfo = loader.readFile(userInfoFile);
       String loadedStatistics = loader.readFile(userStatisticFile);
        ArrayList<String> statisticsLines = parser.getStringArray(loadedStatistics,"\n");
        ArrayList<String> usersInfoArray =parser.getStringArray(loadedInfo,"\n");
        String currentDate = new CurrentDate().getCurrentDate();
        boolean isRewrite = false;
        for (int index = 0; index < usersInfoArray.size(); index++) {
            if (parser.getStringArray(usersInfoArray.get(index), " ").get(0).equals
                    (parser.getStringArray(userInfo, " ").get(0))){
                usersInfoArray.set(index, userInfo);
                infoSaver.rewriteAllFile(usersInfoArray);
                String statisticLine = String.format("%s/%s %d",statisticsLines.get(index),currentDate,scores);
                statisticsLines.set(index,statisticLine);
                statisticSaver.rewriteAllFile(statisticsLines);
                isRewrite = true;
                break;
            }
        }
        if (!isRewrite){
            infoSaver.writeNewLine(userInfo);
            String firstStatisticLine = String.format("%s/%s %d",login,currentDate,scores );
            statisticSaver.writeNewLine(firstStatisticLine);
        }
    }
}

